<?php
namespace app\middleware;

use app\controller\Base;

use tinywan\JWT;
use tinywan\exception\JWTTokenExpiredException;

use think\facade\Log;
/**
 * 登录鉴权中间件
 * Description: 用于控制接口权限
 */

class CheckJwt extends Base
{
    /**
     * 检测登录中间件
     *
     * @param \think\Request $request
     * @param \Closure $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        $response = $next($request);
        // 获取请求头上的jwtToken
        // $token = $request->header('Authorization');
        try {
            // //可验证token, 并获取token中的payload部分
            // $payload = \tinywan\JWT::getExtend();
            // Log::info(var_export($payload, true));
            // if (Jwt::getTokenExp() < 0) {
            //     return $this->createErrMsg([],'token已过期,请刷新!',401);
            // }
            $token = Jwt::verify();
            // Jwt::verify(); JWTTokenExpiredException
        }catch (JWTTokenExpiredException $e){
            return $this->createErrMsg([],'身份会话过期,请刷新token!',401);
        }catch (\Exception $e){
            return $this->createErrMsg([],$e->getMessage(),400);
        }

        return $response;
    }
}